using System;
using UnityEngine;

public class AirCurtain : SingletonMonoBehaviour<AirCurtain>
{
    public float _initialSpeed;
    public float acceleration;
    public float _maxSpeed;
    public int _maxExposureDuration;
    private float _currectSpeed;
    private int _fallTime;
    private int _duration;
    public float Acceleration { get => acceleration; set => acceleration = Math.Max(value, 0); }
    public float CurrectSpeed { get => _currectSpeed; set => _currectSpeed = Math.Max(value, 0); }
    public int Duration { get => _duration; set => _duration = Math.Max(value, 0); }
    public float Height { get => gameObject.transform.position.y; }

    public bool _isDisplayInfo;

    private void Awake()
    {
        INI_Singleton();
        Duration = 0;
    }

    private void Start()
    {
        CurrectSpeed = _initialSpeed;
    }

    private void Update()
    {
        CheckHeigth();
        Fall();
    }

    private void CheckHeigth()
    {
        if (gameObject.transform.position.y < MainRole.Instance.transform.position.y)
        {
            if (++Duration >= _maxExposureDuration) GameManager.Instance.GameOver();
        }
        else
        {
            Duration = 0;
        }
    }

    private void Fall()
    {
        CurrectSpeed = Math.Min(_initialSpeed + (++_fallTime) * Acceleration, _maxSpeed);
        Vector3 pos = gameObject.transform.position;
        gameObject.transform.position = new Vector3(pos.x, pos.y - CurrectSpeed * Time.deltaTime, pos.z);
    }

    public void Repaired()
    {
        _fallTime = 0;
        CurrectSpeed = _initialSpeed;
    }

    public void Load(float height, float saveSpeed)
    {
        gameObject.transform.position = new Vector3(0, height, 0);
        CurrectSpeed = saveSpeed;
    }

    private void OnDisable()
    {

    }

    //private void OnGUI()
    //{
    //    GUIStyle fontStyle = new GUIStyle();
    //    fontStyle.fontSize = 20;
    //    fontStyle.normal.background = null;
    //    fontStyle.normal.textColor = Color.white;
    //    fontStyle.alignment = TextAnchor.MiddleCenter;
    //    string statement = "Speed: " + CurrectSpeed;
    //    if (_isDisplayInfo) GUI.Label(new Rect(30, 50, 200, 100), statement, fontStyle);
    //}
}
